package vip.zhenzicheng.design_patterns.singleton.lazy;

/**
 * DCL双端检锁 但还存在指令重排
 *
 * @author zhenzicheng
 * @date: 2022-05-24 23:13
 */
public class SingletonWithDoubleCheckAndSyncLock {
  private static SingletonWithDoubleCheckAndSyncLock INSTANCE;

  private SingletonWithDoubleCheckAndSyncLock() {
  }

  public static SingletonWithDoubleCheckAndSyncLock getInstance() {
    if (INSTANCE == null) {
      synchronized (SingletonWithDoubleCheckAndSyncLock.class) {
        if (INSTANCE == null) {
          // 隐患：多线程环境下由于重排序，该对象可能还未完成初始化就被读取
          INSTANCE = new SingletonWithDoubleCheckAndSyncLock();
        }
      }
    }
    return INSTANCE;
  }
}
